package com.longsunhd.yum.buffalo.util;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.longsunhd.yum.buffalo.R;


public class ViewUtils {
    private static final String TAG = "ViewUtils";
    private static int viewHeight=0;
    public static int  getViewHeight(Activity activity, final View view){
        ViewTreeObserver viewTreeObserver=view.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int[] location = new int[2];
                view.getLocationOnScreen(location);
                int x = location[0];
                int y = location[1];
                 viewHeight= y;


            }
        });
        return viewHeight;
    }
    public static int  getViewWidth(Activity activity, final View view){
        ViewTreeObserver viewTreeObserver=view.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int[] location = new int[2];
                view.getLocationOnScreen(location);
                int x = location[0];
                int y = location[1];
                viewHeight=x;

            }
        });
        return viewHeight;
    }
    public static void setTextViewColor  (Context context, TextView textView, int colorInt) {
        textView.setTextColor(context.getResources()
                .getColor(colorInt));
    }

    public static void setImageDrable(ImageView imageView, int id){
        imageView.setImageResource( id );

    }


    public static  void openBrowser(Context context, String url){
        final Intent intent = new Intent();
        try {
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            // 注意此处的判断intent.resolveActivity()可以返回显示该Intent的Activity对应的组件名
            // 官方解释 : Name of the component implementing an activity that can display the intent
            if (intent.resolveActivity(context.getPackageManager()) != null) {
                final ComponentName componentName = intent.resolveActivity(context.getPackageManager());
                // 打印Log   ComponentName到底是什么

                context.startActivity(Intent.createChooser(intent, "请选择浏览器"));
            } else {
                Toast.makeText(context.getApplicationContext(), "请下载浏览器", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static View getLayoutView(Context context,int layoutID){
      return LayoutInflater.from(context).inflate(layoutID, null);
    }

}
